package com.yqyd.web.controller.system;

import com.yqyd.common.annotation.Anonymous;
import com.yqyd.common.core.domain.R;
import com.yqyd.system.domain.SysConfig;
import com.yqyd.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yqyd.common.config.YqydConfig;
import com.yqyd.common.utils.StringUtils;

import javax.annotation.Resource;

/**
 * 首页
 *
 * @author Dhq
 */
@RestController
public class SysIndexController
{
    /** 系统基础配置 */
    @Autowired
    private YqydConfig yqydConfig;

    @Resource
    private ISysConfigService configService;

    private static final String KEY = "hy.index.text";



    private static final String ADDRESS_KEY = "hy.wechat.address";

    private static final Long ID = 103L;

    private static final Long ADDRESS_ID = 105L;

    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index()
    {
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", yqydConfig.getName(), yqydConfig.getVersion());
    }

    @Anonymous
    @GetMapping("/TipText")
    public R<String> TipText(){
        return R.ok(configService.selectConfigByKey(KEY));
    }

    @Anonymous
    @GetMapping("/AddressText")
    public R<String> AddressText(){
        return R.ok(configService.selectConfigByKey(ADDRESS_KEY));
    }

    @PutMapping("/EditTipText")
    public R<String> EditTipText(@RequestParam String value){
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigId(ID);
        sysConfig.setConfigValue(value);
        configService.updateConfig(sysConfig);
        return R.ok();
    }

    @PutMapping("/EditAddressText")
    public R<String> EditAddressText(@RequestParam String value){
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigId(ADDRESS_ID);
        sysConfig.setConfigValue(value);
        configService.updateConfig(sysConfig);
        return R.ok();
    }
}
